/*
 * @Author: your name
 * @Date: 2019-11-07 14:08:01
 * @LastEditTime: 2020-03-25 20:19:36
 * @LastEditors: huacong
 * @Description: In User Settings Edit
 * @FilePath: \pc\src\pages\ValueManage\PriceManage\ApproveMobile\models\index.js
 */
import _ from 'lodash';
import * as api from '@/services/ValueApprovelMgr';
import * as utils from '../utils';

const Model = {
  namespace: 'ApproveMobile',
  state: {
    basicInfo: [],
    versionReporting: [],
    tableDate1: [],
    tableDate2: [],
    expandedRowKeys: [],
    tabs: [],
  },
  effects: {
    // 基本信息
    *basicInfo({ payload, callback }, { call, put }) {
      const response = yield call(api.appBasicInfoUsingGET, payload);
      if (response && response.code === 1) {
        const basicInfo = response.data;
        yield put({
          type: 'save',
          payload: { basicInfo },
        });
      }
      if (callback) callback();
    },
    // 本次版本填报
    *versionReporting({ payload, callback }, { call, put }) {
      const versionReporting = [];
      const response = yield call(api.appApprovedInfoUsingGET, payload);
      if (response && response.code === 1) {
        const cloneData = _.cloneDeep(response.data);
        const keys = Object.keys(cloneData);
        _.each(keys, val => {
          versionReporting.push({
            name: val,
            value: cloneData[val],
          });
        });
        yield put({
          type: 'save',
          payload: { versionReporting },
        });
      }
      if (callback) callback();
    },
    // 全盘货值概览
    *valueOlive({ payload, callback }, { call, put }) {
      const tabs = [];
      const response = yield call(api.appNewAllgoodsValueReviewUsingGET, payload);
      if (response && response.code === 1) {
        const tableDate1 = _.cloneDeep(response.data);
        if (!_.isEmpty(tableDate1)) {
          _.each(tableDate1, v => {
            const x = v;
            if (v.versionName === '调整前') {
              x.key = 0;
              x.type = 'adjust_before';
            } else if (v.versionName === '大定价版') {
              x.key = 1;
              x.type = 'pricing';
            } else if (v.versionName === '战规版') {
              x.key = 2;
              x.type = 'strategy';
            } else if (v.versionName === '投资版') {
              x.key = 3;
              x.type = 'investment';
            }
          });
          tableDate1.sort((a, b) => a.key - b.key);
          _.forEach(tableDate1, v =>
            tabs.push({
              title: `VS ${v.versionName}`,
              key: v.type,
            }),
          );
        }
        yield put({
          type: 'save',
          payload: { tableDate1, tabs },
        });
      }
      if (callback) callback(response);
    },
    // 全盘货值差异
    *allgoodsValueDetail({ payload, callback }, { call, put }) {
      yield put({
        type: 'save',
        payload: {
          tableDate2: [],
        },
      });
      const response = yield call(api.appAllgoodsValueDetailUsingGET, payload);
      if (!_.isEmpty(response.data)) {
        const { data } = response;
        const expandedRowKeys = utils.getExpandedRowKeys(data);
        const treeData = utils.buildTree(data);
        yield put({
          type: 'save',
          payload: {
            tableDate2: treeData,
            expandedRowKeys,
          },
        });
      }
      if (callback) callback();
    },
  },
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
};
export default Model;
